package com.xuecheng.media.api;

import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * Date:2023/8/15
 * Author:丐版小杨哥
 * Description:
 */
@RestController
public class BigFileController {
    @Autowired
    private MediaFileService mediaFileService;
    /**
     * 判断文件是否存在
     * @param fileMd5 文件的md5值
     * @return 返回结果
     */
    @ApiOperation("文件上传前检查文件")
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> checkfile(@RequestParam("fileMd5") String fileMd5){
        return mediaFileService.checkfile(fileMd5);
    }

    /**
     * 检查分块
     * @param fileMd5
     * @param chunk
     * @return
     */
    @ApiOperation("检查分块是否存在")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkChunk(@RequestParam("fileMd5") String fileMd5,
                           @RequestParam("chunk") int chunk){
        return mediaFileService.checkChunk(fileMd5,chunk);
    }

    /**
     * 上传分块
     * @param file
     * @param fileMd5
     * @param chunk
     * @return
     */
    @ApiOperation("上传分块文件")
    @RequestMapping("/upload/uploadchunk")
    public RestResponse<Boolean> uploadchunk(
            @RequestParam("file") MultipartFile file,
            @RequestParam("fileMd5") String fileMd5,
            @RequestParam("chunk")int chunk) throws Exception {
        File tempFile = File.createTempFile("minio", ".temp");
        file.transferTo(tempFile);
        Long companyId = 1232141425L;
        //文件路径
        String localFilePath = tempFile.getAbsolutePath();
        RestResponse<Boolean> uploadchunk = mediaFileService.uploadchunk(fileMd5, chunk, localFilePath);
        tempFile.delete();
        return uploadchunk;
    }

    /**
     *合并分块
     * @param fileMd5
     * @param fileName
     * @param chunkTotal
     * @return
     */
    @ApiOperation("合并分块文件")
    @PostMapping("/upload/mergechunks")
    public RestResponse<Boolean> mergechunks(@RequestParam("fileMd5") String fileMd5,
                                           @RequestParam("fileName") String fileName,
                                           @RequestParam("chunkTotal")int  chunkTotal){
        Long companyId = 1232141425L;
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        uploadFileParamsDto.setFilename(fileName);
        uploadFileParamsDto.setTags("视频文件");
        uploadFileParamsDto.setFileType("001002");
        RestResponse<Boolean> restResponse = mediaFileService.mergechunks(companyId, fileMd5, chunkTotal,uploadFileParamsDto);
        return restResponse;
    }
}